﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Core
{
    /// <summary>
    /// web.config操作类
    /// Copyright (C) 
    /// </summary>
    public sealed class ConfigHelper
    {
        #region GetConfigString(string key)得到AppSettings中的配置字符串信息
        /// <summary>
        /// 得到AppSettings中的配置字符串信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetConfigString(string key)
        {
            var objModel = ConfigurationManager.AppSettings[key];
            return string.IsNullOrWhiteSpace(objModel) ? string.Empty : objModel.ToString();
        }
        #endregion

        #region GetConfigBool(string key) 得到AppSettings中的配置Bool信息
        /// <summary>
        /// 得到AppSettings中的配置Bool信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool GetConfigBool(string key)
        {
            bool result = false;
            string cfgVal = GetConfigString(key);
            if (null != cfgVal && string.Empty != cfgVal)
            {
                try
                {
                    result = bool.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    result = false;
                }
            }
            return result;
        }
        #endregion

        #region GetConfigDecimal(string key) 得到AppSettings中的配置Decimal信息
        /// <summary>
        /// 得到AppSettings中的配置Decimal信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static decimal GetConfigDecimal(string key)
        {
            decimal result = 0;
            string cfgVal = GetConfigString(key);
            if (null != cfgVal && string.Empty != cfgVal)
            {
                try
                {
                    result = decimal.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    result = 0;
                }
            }

            return result;
        }
        #endregion

        #region 得到AppSettings中的配置int信息
        /// <summary>
        /// 得到AppSettings中的配置int信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static int GetConfigInt(string key)
        {
            int result = 0;
            string cfgVal = GetConfigString(key);
            if (null != cfgVal && string.Empty != cfgVal)
            {
                try
                {
                    result = int.Parse(cfgVal);
                }
                catch (FormatException)
                {
                    result = 0;
                }
            }

            return result;
        }
        #endregion

        #region 设置AppSettings中的配置字符串信息
        /// <summary>
        /// 设置AppSettings中的配置字符串信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="vaule"></param>
        public static void SetConfigString(string key, string vaule)
        {
            try
            {
                //读取程序集的配置文件
                string assemblyConfigFile = Assembly.GetEntryAssembly().Location;
                string appDomainConfigFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                //获取appSettings节点
                AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
                //删除name，然后添加新值
                appSettings.Settings.Remove(key);
                appSettings.Settings.Add(key, vaule);
                //保存配置文件
                config.Save();

                Configuration configLocal = System.Configuration.ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath.ToLower());
                AppSettingsSection conSection = (AppSettingsSection)configLocal.GetSection("appSettings");
                conSection.Settings[key].Value = vaule;
                configLocal.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
            }
            catch (Exception ex)
            {
                Log.errorlog(ex);
            }
        }
        #endregion
    }
}
